package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.*;
import com.bbu.javaexam.domain.vo.ReadExamStudentVO;
import com.bbu.javaexam.domain.vo.ReadexamdataVO;
import com.bbu.javaexam.service.ExamManageService;
import com.bbu.javaexam.service.ReadexamService;
import com.bbu.javaexam.service.ScoreService;
import com.bbu.javaexam.service.StudentService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Alex
 * @version 1.0
 * @classname ReadexamController
 * @description
 * @date 2022/04/15 13:49
 */
@RestController
@RequestMapping("/readexam")
public class ReadexamController {

    @Autowired
    private ReadexamService readexamService;

    @Autowired
    private ExamManageService examManageService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private ScoreService scoreService;

    @PostMapping("/save")
    public RespData save(@RequestBody Readexam readexam) {
        LambdaQueryWrapper<Readexam> lam = new LambdaQueryWrapper<>();
        lam.eq(Readexam::getStudentid, readexam.getStudentid());
        lam.eq(Readexam::getType, readexam.getType());
        lam.eq(Readexam::getQuestionid, readexam.getQuestionid());
        Readexam one = readexamService.getOne(lam);
        if (!ObjectUtils.isEmpty(one)) {
            readexamService.remove(lam);
        }
        if ("1".equals(readexam.getType())) {
            switch (readexam.getAnswerid()) {
                case "1":
                    readexam.setAnswerid("A");
                    break;
                case "2":
                    readexam.setAnswerid("B");
                    break;
                case "3":
                    readexam.setAnswerid("C");
                    break;
                case "4":
                    readexam.setAnswerid("D");
                    break;
            }
        } else if ("3".equals(readexam.getType())) {
            switch (readexam.getAnswerid()) {
                case "1":
                    readexam.setAnswerid("对");
                    break;
                case "2":
                    readexam.setAnswerid("错");
                    break;
            }
        }
        readexamService.save(readexam);
        return RespDataHandler.success();
    }

    @GetMapping("/selectexams")
    public RespData selectexam() {
        QueryWrapper<Readexam> lam = new QueryWrapper<>();
        lam.select("DISTINCT(examCode)");
        List<Map<String, Object>> maps = readexamService.listMaps(lam);
        List<ReadexamdataVO> readexamdataVOS = maps.stream().map((item) -> {
            ReadexamdataVO readexamdataVO = new ReadexamdataVO();
            String examCode = (String) item.get("examCode");
            LambdaQueryWrapper<ExamManage> lam1 = new LambdaQueryWrapper<>();
            lam1.eq(ExamManage::getExamCode, examCode);
            ExamManage examManage = examManageService.getOne(lam1);
            readexamdataVO.setExamname(examManage.getDescription());
            QueryWrapper<Readexam> lambdaQueryWrapper = new QueryWrapper<>();
            lambdaQueryWrapper.eq("examcode", examCode);
            lambdaQueryWrapper.select("DISTINCT(studentid)");
            lambdaQueryWrapper.groupBy("studentid");
            readexamdataVO.setExamCode(examCode);
            int count = readexamService.count(lambdaQueryWrapper);
            readexamdataVO.setNum(count);
            return readexamdataVO;
        }).collect(Collectors.toList());
        return RespDataHandler.success(readexamdataVOS);
    }

    @GetMapping("/selectstudents/{examcode}/{size}/{pageSize}")
    public RespData selectstudents(@PathVariable("examcode") String examcode, @PathVariable("size") Integer size, @PathVariable("pageSize") Integer pageSize) {
        Page<Readexam> page = new Page(size, pageSize);
        Page<ReadExamStudentVO> page1 = new Page<>();
        LambdaQueryWrapper<Readexam> lamn = new LambdaQueryWrapper<>();
        lamn.eq(Readexam::getExamcode, examcode);
        lamn.groupBy(Readexam::getStudentid);
        readexamService.page(page, lamn);
        BeanUtils.copyProperties(page, page1, "records");
        List<Readexam> records = page.getRecords();
        List<ReadExamStudentVO> readExamStudentVOList = records.stream().map((item) -> {
            ReadExamStudentVO readExamStudentVO = new ReadExamStudentVO();
            String studentid = item.getStudentid();
            LambdaQueryWrapper<Student> lam = new LambdaQueryWrapper<>();
            lam.eq(Student::getStudentId, studentid);
            Student student = studentService.getOne(lam);
            readExamStudentVO.setStudentid(studentid);
            readExamStudentVO.setUsername(student.getStudentName());
            LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
            scoreLambdaQueryWrapper.eq(Score::getExamCode, examcode);
            scoreLambdaQueryWrapper.eq(Score::getStudentId, studentid);
            Score score = scoreService.getOne(scoreLambdaQueryWrapper);
            readExamStudentVO.setIsread(ObjectUtils.isEmpty(score.getIsread()) ? "未阅卷" : "已阅卷");
            readExamStudentVO.setRscore(score.getEtScore());
            readExamStudentVO.setScore(score.getScore() == null ? 0 : score.getScore());
            return readExamStudentVO;
        }).collect(Collectors.toList());
        page1.setRecords(readExamStudentVOList);
        return RespDataHandler.success(page1);
    }

    @GetMapping("/selectanswer/{examcode}/{studentid}/{questionid}/{type}")
    public RespData selectanswer(@PathVariable("examcode") String examcode, @PathVariable("studentid") String studentid,
                                 @PathVariable("questionid") String questionid, @PathVariable("type") String type) {
        LambdaQueryWrapper<Readexam> lam = new LambdaQueryWrapper<>();
        lam.eq(Readexam::getExamcode, examcode);
        lam.eq(Readexam::getStudentid, studentid);
        lam.eq(Readexam::getType, type);
        lam.eq(Readexam::getQuestionid, questionid);
        Readexam readexam = readexamService.getOne(lam);
        if ("2".equals(type)) {
            if (!ObjectUtils.isEmpty(readexam)) {
                return RespDataHandler.success(readexam.getAnwer());
            } else {
                return RespDataHandler.success("未作答");
            }
        } else {
            if (!ObjectUtils.isEmpty(readexam)) {
                return RespDataHandler.success(readexam.getAnswerid());
            } else {
                return RespDataHandler.success("未作答");
            }
        }

    }

    @GetMapping("/savescore/{examcode}/{studentid}/{fscore}")
    public RespData savescore(@PathVariable("examcode") String examcode, @PathVariable("studentid") String studentid,
                              @PathVariable("fscore") Integer fscore) {
        Score score = new Score();
        score.setScore(fscore);
        score.setIsread("已阅卷");
        LambdaQueryWrapper<Score> lam = new LambdaQueryWrapper<>();
        lam.eq(Score::getExamCode, examcode);
        lam.eq(Score::getStudentId, studentid);
        scoreService.update(score, lam);
        return RespDataHandler.success();
    }

}
